package com.xoa.controller.workflow;

import com.xoa.model.workflow.FlowRelation;
import com.xoa.service.workflow.flowModelAssociationSet.FlowAssociationSetService;
import com.xoa.util.CookiesUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.dataSource.ContextHolder;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@Scope(value = "prototype")
@RequestMapping("/flowModelAssociationSet")

/**
 * 设置关联应用Controller
 * 王禹萌
 * 2018-08-20
 */
public class FlowAssociationSetController {
	@Resource
	private FlowAssociationSetService flowAssociationSetService;

	/**
	 * 报表设置映射
	 * wangyumeng
	 * 2018-08-20
	 *
	 * @param model
	 * @param request
	 * @param formId
	 * @return
	 */
	@RequestMapping("/flowModelAssociationSetList")
	public String previews(Model model, HttpServletRequest request, Integer formId) {
		Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
		model.addAttribute("formId", formId);
		return "app/workflow/flowModelAssociationSet/reportSettingsList";
	}

	/**
	 * 新建流程、模块设置关联应用
	 * 王禹萌
	 * 2018-08-01 14：06
	 *
	 * @param flowRefNo             排序号,
	 * @param flowId                流程ID',
	 * @param flowRelName           关联关系名称',
	 * @param relationType          关联类型（1-固定关联卡片2-关联流程）',
	 * @param relationFlowId;       关联字段',
	 * @param relationStatus        关联状态(0-停用,1-必选,可选状态无效)',
	 * @param flowRange             关联人员范围(0为所有，1为经办)',
	 * @param timeRange             时间范围(上月1、上季度2、上年度3、本月4、本季度5、本年6)',
	 * @param dataDirection         数据方向(0-单向,1-双向)',
	 * @param dataDirection;        '关联描述',
	 * @param mapping;              '数据关系映射',
	 * @param mappingDesc           中文数据关联映射',
	 * @return
	 */
	@RequestMapping("/newFlowModelAssociationSet")
	@ResponseBody
	public ToJson<FlowRelation> newFlowModelAssociationSet(
			@RequestParam(required = false) Integer flowRefNo,
			@RequestParam(required = false) Integer flowId,
			@RequestParam(required = false) String flowRelName,
			@RequestParam(required = false) Integer relationType,
			@RequestParam(required = false) String relationFlowId,
			@RequestParam(required = false) Integer relationStatus,
			@RequestParam(required = false) Integer flowRange,
			@RequestParam(required = false) String timeRange,
			@RequestParam(required = false) Integer dataDirection,
			@RequestParam(required = false) String flowRelDesc,
			@RequestParam(required = false) String mapping,
			@RequestParam(required = false) String mappingDesc
	) {

		return flowAssociationSetService.newFlowModelAssociationSet(flowRefNo, flowId, flowRelName, relationType, relationFlowId, relationStatus, flowRange, timeRange, dataDirection, flowRelDesc, mapping, mappingDesc);

	}

	/**
	 * 新建模块关联
	 * 王禹萌，
	 * 2018-10-31 13：22：22
	 * @param flowId
	 * @param relationType
	 * @return
	 */
	@RequestMapping("/newModelAssociationSet")
	@ResponseBody
	public ToJson<FlowRelation> newModelAssociationSet(
			@RequestParam(required = false) Integer flowId,
			@RequestParam(required = false) Integer relationType,
			@RequestParam(required = false) String relationFlowId

	) {
		return flowAssociationSetService.newModelAssociationSet(flowId,relationType,relationFlowId);
	}

	/**
	 * 查询流程模块关联列表
	 * 王禹萌
	 * 2018-08-21 12:42
	 *
	 * @param pageSize
	 * @param page
	 * @param useFlag
	 * @return
	 */
	@RequestMapping("/queryFlowModelAssociationSet")
	@ResponseBody
	public ToJson<FlowRelation> queryFlowModelAssociationSet(@RequestParam(required = false) Integer pageSize,
															 @RequestParam(required = false) Integer page,
															 @RequestParam(required = false) Boolean useFlag,
															 @RequestParam(required = false) Integer flowId) {
		return flowAssociationSetService.queryFlowModelAssociationSet(pageSize, page, useFlag, flowId);

	}

	/**
	 * 流程模块关联编辑
	 * 王禹萌
	 * 2018-08-30
	 *
	 * @param flowRefNo             排序号,
	 * @param flowId                流程ID',
	 * @param flowRelName           关联关系名称',
	 * @param relationType          关联类型（1-固定关联卡片2-关联流程）',
	 * @param relationFlowId;关联字段',
	 * @param relationStatus        关联状态(0-停用,1-必选,可选状态无效)',
	 * @param flowRange             关联人员范围(0为所有，1为经办)',
	 * @param timeRange             时间范围(上月1、上季度2、上年度3、本月4、本季度5、本年6)',
	 * @param dataDirection         数据方向(0-单向,1-双向)',
	 * @param dataDirection;        '关联描述',
	 * @param mapping;              '数据关系映射',
	 * @param mappingDesc           中文数据关联映射',
	 * @return
	 */
	@RequestMapping("/editFlowModelAssociationSet")
	@ResponseBody
	public ToJson<FlowRelation> editFlowModelAssociationSet(
			@RequestParam(required = false) Integer flowRelId,
			@RequestParam(required = false) Integer flowRefNo,
			@RequestParam(required = false) Integer flowId,
			@RequestParam(required = false) String flowRelName,
			@RequestParam(required = false) Integer relationType,
			@RequestParam(required = false) String relationFlowId,
			@RequestParam(required = false) Integer relationStatus,
			@RequestParam(required = false) Integer flowRange,
			@RequestParam(required = false) String timeRange,
			@RequestParam(required = false) Integer dataDirection,
			@RequestParam(required = false) String flowRelDesc,
			@RequestParam(required = false) String mapping,
			@RequestParam(required = false) String mappingDesc
	) {
		return flowAssociationSetService.editFlowModelAssociationSet(flowRelId, flowRefNo, flowId, flowRelName, relationType, relationFlowId, relationStatus, flowRange, timeRange, dataDirection, flowRelDesc, mapping, mappingDesc);

	}

	/**
	 * 删除流程关联应用
	 * 王禹萌
	 * 2018-08-22
	 *
	 * @param id
	 * @return
	 */
	@RequestMapping("/deleteFlowModelAssociationSet")
	@ResponseBody
	public ToJson<FlowRelation> deleteFlowModelAssociationSet(@RequestParam(required = false) Integer id) {
		return flowAssociationSetService.deleteFlowModelAssociationSet(id);

	}

	/*
 * 流程-查看详情-查询该流程关联符合的流程
 * 王禹萌
 * 2018-09-03 13:59:56
 * @param flowId
 * @return
 */
	@RequestMapping("/queryFlowAssociation")
	@ResponseBody
	public ToJson<Map<String, Object>> queryFlowAssociationFlowRunPrcs(@RequestParam(required = true) Integer flowId, String type) {
		return flowAssociationSetService.queryFlowAssociationFlowRunPrcs(flowId, type);
	}

	/**
	 * 获取流程关联信息
	 * @param flowId
	 * @param runId

	 * @return
	 */
	@RequestMapping("/getFlowRunInfo")
	@ResponseBody
	public ToJson getFlowRunInfo(Integer flowId, Integer runId,String printModel,String attributesStr) {
		return flowAssociationSetService.getFlowRunInfo(flowId,runId,1,printModel,attributesStr);
	}
}
